package hyperNexusModel

import (
	"github.com/spaolacci/murmur3"
	"github.com/wzh200x/hyperNexus/hyperNexusType"
)

// ShardConfig 分表配置
type ShardConfig struct {
	ShardNum           int                              // 分表数量
	GetShardSuffixFunc hyperNexusType.T_ShardSuffixFunc // 自定义分表后缀计算函数
}

func ShardSuffixDefault(keyValue string, shardNum int) int {
	// 计算 MurmurHash3 64位哈希值
	hash := murmur3.Sum64([]byte(keyValue))
	// 取模运算（确保分表数为质数）
	return int(hash % uint64(shardNum))
}

func NewDefaultShardConfig() *ShardConfig {
	return &ShardConfig{
		ShardNum:           hyperNexusType.TableShardingNum,
		GetShardSuffixFunc: ShardSuffixDefault,
	}
}

func NewShardConfig(shardNum int, getShardSuffixFunc func(keyValue string, shardNum int) int) *ShardConfig {
	return &ShardConfig{
		ShardNum:           shardNum,
		GetShardSuffixFunc: getShardSuffixFunc,
	}
}
